home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte27 / mditest.c < prev    next >
C/C++ Source or Header  |  1995-12-22  |  10KB  |  291 lines

  1.  
  2. #include <windows.h>  
  3. #include "mditest.h"  
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HWND      hWndClient = NULL;
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName  = "MyMDIApp";
  20. LPCTSTR lpszChild    = "MDIChild";
  21. LPCTSTR lpszTitle    = "MDI Test Application"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    // Register the main application window class.
  35.    //............................................
  36.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  37.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  38.    wc.cbClsExtra    = 0;                      
  39.    wc.cbWndExtra    = 0;                      
  40.    wc.hInstance     = hInstance;              
  41.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  42.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  43.    wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1);
  44.    wc.lpszMenuName  = lpszAppName;              
  45.    wc.lpszClassName = lpszAppName;              
  46.  
  47.    if ( IS_WIN95 )
  48.    {
  49.       if ( !RegisterWin95( &wc ) )
  50.          return( FALSE );
  51.    }
  52.    else if ( !RegisterClass( &wc ) )
  53.       return( FALSE );
  54.  
  55.    // Register the window class for the MDI child windows.
  56.    //.....................................................
  57.    wc.lpfnWndProc   = (WNDPROC)ChildWndProc;
  58.    wc.hIcon         = LoadIcon( hInstance, lpszChild );
  59.    wc.hCursor       = LoadCursor( NULL, IDC_ARROW );
  60.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  61.    wc.lpszMenuName  = NULL;
  62.    wc.lpszClassName = lpszChild;
  63.  
  64.    if ( IS_WIN95 )
  65.    {
  66.       if ( !RegisterWin95( &wc ) )
  67.          return( FALSE );
  68.    }
  69.    else if ( !RegisterClass( &wc ) )
  70.       return( FALSE );
  71.  
  72.    hInst = hInstance; 
  73.  
  74.    // Create the main application window.
  75.    //....................................
  76.    hWnd = CreateWindow( lpszAppName, 
  77.                         lpszTitle,    
  78.                         WS_OVERLAPPEDWINDOW, 
  79.                         CW_USEDEFAULT, 0, 
  80.                         CW_USEDEFAULT, 0,  
  81.                         NULL,              
  82.                         NULL,              
  83.                         hInstance,         
  84.                         NULL               
  85.                       );
  86.  
  87.    if ( !hWnd ) 
  88.       return( FALSE );
  89.  
  90.    ShowWindow( hWnd, nCmdShow ); 
  91.    UpdateWindow( hWnd );         
  92.  
  93.    while( GetMessage( &msg, NULL, 0, 0) )   
  94.    {
  95.       if ( hWndClient && TranslateMDISysAccel( hWndClient, &msg ) )
  96.          continue;
  97.  
  98.       TranslateMessage( &msg ); 
  99.       DispatchMessage( &msg );  
  100.    }
  101.  
  102.    return( msg.wParam ); 
  103. }
  104.  
  105.  
  106. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  107. {
  108.    WNDCLASSEX wcex;
  109.  
  110.    wcex.style         = lpwc->style;
  111.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  112.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  113.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  114.    wcex.hInstance     = lpwc->hInstance;
  115.    wcex.hIcon         = lpwc->hIcon;
  116.    wcex.hCursor       = lpwc->hCursor;
  117.    wcex.hbrBackground = lpwc->hbrBackground;
  118.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  119.    wcex.lpszClassName = lpwc->lpszClassName;
  120.  
  121.    // Added elements for Windows 95.
  122.    //...............................
  123.    wcex.cbSize = sizeof(WNDCLASSEX);
  124.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  125.                             IMAGE_ICON, 16, 16,
  126.                             LR_LOADREALSIZE );
  127.             
  128.    return RegisterClassEx( &wcex );
  129. }
  130.  
  131. // An ID that is different than all menu ids.
  132. //...........................................
  133. #define ID_CHILDWINDOW 1000 
  134.  
  135. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  136. {
  137.    switch( uMsg )
  138.    {
  139.       case WM_CREATE :
  140.               {
  141.                  CLIENTCREATESTRUCT ccs;
  142.     
  143.                  // Assign the 'Window' menu.
  144.                  //..........................             
  145.                  ccs.hWindowMenu  = GetSubMenu( GetMenu( hWnd ), 1 );
  146.                  ccs.idFirstChild = ID_CHILDWINDOW;
  147.  
  148.                  // Create the client window.
  149.                  //..........................
  150.                  hWndClient = CreateWindowEx( WS_EX_CLIENTEDGE, 
  151.                                               "MDICLIENT", NULL,
  152.                                               WS_CHILD | WS_CLIPCHILDREN, 
  153.                                               0, 0, 0, 0,
  154.                                               hWnd, (HMENU)0xCA0, hInst, &ccs);
  155.  
  156.                  ShowWindow( hWndClient, SW_SHOW );
  157.               }
  158.               break;
  159.  
  160.       case WM_SIZE :
  161.               // Size the client window to the size of the client
  162.               // area of the main application window.
  163.               //.................................................
  164.               MoveWindow( hWndClient, 0, 0, LOWORD( lParam ), 
  165.                                             HIWORD( lParam ), TRUE );
  166.               break;
  167.      
  168.       case WM_COMMAND :
  169.               switch( LOWORD( wParam ) )
  170.               {
  171.                  case IDM_NEW :
  172.                      {
  173.                         HWND hWndChild;
  174.  
  175.                         // Create a new child window.
  176.                         //...........................
  177.                         hWndChild = CreateMDIWindow( (LPTSTR)lpszChild, 
  178.                                          "Document", 0L,
  179.                                          CW_USEDEFAULT, CW_USEDEFAULT, 
  180.                                          CW_USEDEFAULT, CW_USEDEFAULT,
  181.                                          hWndClient, hInst, 0L);
  182.  
  183.                         ShowWindow( hWndChild, SW_SHOW );
  184.                      }
  185.                      break;
  186.  
  187.                  case IDM_CLOSE :
  188.                      {
  189.                         HWND hActiveWnd;
  190.                         
  191.                         // Close the active child window.
  192.                         //...............................
  193.                         hActiveWnd = (HWND)SendMessage( hWndClient, 
  194.                                                         WM_MDIGETACTIVE, 0, 0 );
  195.                         if ( hActiveWnd )
  196.                            SendMessage( hWndClient, WM_MDIDESTROY, (WPARAM)hActiveWnd, 0 );
  197.                      }
  198.                      break;
  199.  
  200.                  case IDM_CASCADE :
  201.                         if ( IS_WIN95 )
  202.                            CascadeWindows( hWndClient, MDITILE_SKIPDISABLED,
  203.                                            NULL, 0, NULL );
  204.                         else
  205.                            SendMessage( hWndClient, WM_MDICASCADE, 
  206.                                         MDITILE_SKIPDISABLED, 0 );
  207.                         break;
  208.  
  209.                  case IDM_TILEHORZ :
  210.                         if ( IS_WIN95 )
  211.                            TileWindows( hWndClient, MDITILE_HORIZONTAL, 
  212.                                         NULL, 0, NULL );
  213.                         else
  214.                            SendMessage( hWndClient, WM_MDITILE, 
  215.                                         MDITILE_HORIZONTAL, 0 );
  216.                         break;
  217.  
  218.                  case IDM_TILEVERT :
  219.                         if ( IS_WIN95 )
  220.                            TileWindows( hWndClient, MDITILE_VERTICAL, 
  221.                                         NULL, 0, NULL );
  222.                         else
  223.                            SendMessage( hWndClient, WM_MDITILE, 
  224.                                         MDITILE_VERTICAL, 0 );
  225.                         break;
  226.  
  227.                  case IDM_ARRANGE :
  228.                         ArrangeIconicWindows( hWndClient );
  229.                         break;
  230.  
  231.                  case IDM_ABOUT :
  232.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  233.                         break;
  234.  
  235.                  case IDM_EXIT :
  236.                         DestroyWindow( hWnd );
  237.                         break;
  238.  
  239.                  default :
  240.                         return( DefFrameProc( hWnd, hWndClient, 
  241.                                               uMsg, wParam, lParam ) );      
  242.               }
  243.               break;
  244.       
  245.       case WM_DESTROY :
  246.               PostQuitMessage(0);
  247.               break;
  248.  
  249.       default :
  250.             return( DefFrameProc( hWnd, hWndClient, uMsg, wParam, lParam ) );
  251.    }
  252.  
  253.    return( 0L );
  254. }
  255.  
  256.  
  257. LRESULT CALLBACK ChildWndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  258. {
  259.    switch( uMsg )
  260.    {
  261.       default :
  262.             return( DefMDIChildProc( hWnd, uMsg, wParam, lParam ) );
  263.    }
  264.  
  265.    return( 0L );
  266. }
  267.  
  268.  
  269. LRESULT CALLBACK About( HWND hDlg,           
  270.                         UINT message,        
  271.                         WPARAM wParam,       
  272.                         LPARAM lParam)
  273. {
  274.    switch (message) 
  275.    {
  276.        case WM_INITDIALOG: 
  277.                return (TRUE);
  278.  
  279.        case WM_COMMAND:                              
  280.                if (   LOWORD(wParam) == IDOK         
  281.                    || LOWORD(wParam) == IDCANCEL)    
  282.                {
  283.                        EndDialog(hDlg, TRUE);        
  284.                        return (TRUE);
  285.                }
  286.                break;
  287.    }
  288.  
  289.    return (FALSE); 
  290. }
  291.